<?php namespace App\Domain\BooksInDoor\Service;


use App\Domain\BooksInDoor\Model\BooksInDoor;
use Illuminate\Support\Facades\DB;


class BooksInDoorServiceImpl implements BooksInDoorService
{
    public function returnUpdateRelation($doorid,$bookuid){
        DB::beginTransaction();
        try {
            //添加关系
            $book=BooksInDoor::where('BookEpc',$bookuid)->first();
            if($book){
                $book->delete();
            }
            //添加关系
            BooksInDoor::create([
                'DoorID'=>$doorid,
                'BookEpc'=>$bookuid,
                'InTime'=>time()]);
            DB::commit();
        }catch (\Exception $e) {

            DB::rollback();
            return false;
        }
        return true;
    }

    public function lendUpdateRelation($doorid,$bookuid){
        DB::beginTransaction();
        try {
            //添加关系
            $book=BooksInDoor::where('BookEpc',$bookuid)->first();
            if($book){
                $book->delete();
            }
            DB::commit();
        }catch (\Exception $e) {

            DB::rollback();
            return false;
        }
        return true;


    }

    public function getBookNum($doorid){
        return BooksInDoor::where('DoorID',$doorid)
            ->count();
    }

}